package com.woniu51.finance.controller;

import com.woniu51.finance.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 杨光
 * @date 2022年04月11日 15:57
 */
@Controller
public class FinanceController {


    @Autowired
    private FinanceService financeService;


    @RequestMapping("web/order/pay")
    public String pay(String orderId, HttpServletRequest request){
        Long oid = Long.valueOf(orderId.substring(1));
        String form = financeService.pay(oid);
        request.setAttribute("form",form);
        return "alipay";
    }

    @RequestMapping("/alipay/notify")
    public void returnNotify(HttpServletRequest req, HttpServletResponse resp){

        System.out.println("returnNotify:::"+req.getParameter("out_trade_no"));
        String orderIdStr = req.getParameter("out_trade_no");
        //远程调用订单微服务，把订单的状态修改为已经付款
        Long oid = Long.valueOf(orderIdStr);
        //如果返回的是0 ，可能订单已经失效了，需要走退款的接口。
        //如果返回1，那么订单状态已经成功修改为已支付
        Integer result = financeService.confirmPay(oid);

        try {
            resp.sendRedirect("http://www.wncinema.com");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
